//
//  HCTarBar.swift
//  新浪微博
//
//  Created by fanhoucheng on 16/9/22.
//  Copyright © 2016年 fanhoucheng. All rights reserved.
//

import UIKit

class HCTarBar: UITabBar {
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        addSubview(button)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //创建button,懒加载
    lazy var button: UIButton = {
        let btn = UIButton()
        //设置button的图片
        btn.setImage(#imageLiteral(resourceName: "tabbar_compose_icon_add"), for: UIControlState.normal)
        btn.setImage(#imageLiteral(resourceName: "tabbar_compose_icon_add_highlighted"), for: UIControlState.highlighted)
        
        //设置背景图片
        btn.setBackgroundImage(#imageLiteral(resourceName: "tabbar_compose_button"), for: UIControlState.normal)
        btn.setBackgroundImage(#imageLiteral(resourceName: "tabbar_compose_button_highlighted"), for: UIControlState.highlighted)
        
        return btn
    }()

    //布局子控件
    override func layoutSubviews() {
        super.layoutSubviews()
        
        //所有子控件的frame重写布局
        //获取宽和高
        let ItemW = UIScreen.main.bounds.width / 5
        let ItemH = self.bounds.height
        var index:Int = 0
        for subview in self.subviews {
            if subview .isKind(of: NSClassFromString("UITabBarButton")!) {
                
                subview.frame = CGRect(x: CGFloat(index) * ItemW, y: 0, width: ItemW, height: ItemH)
                
                index += 1
                if index == 2 {
                    index += 1
                }
            }
        }
        
        //设置button的frame
        button.bounds.size = CGSize(width: ItemW, height: ItemH)
        button.center = CGPoint(x: self.center.x, y: self.bounds.height * 0.5)
    }

}
